//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
let { WeToast } = require('../../src/wetoast.js');

Page({
  data: {
    selectIndex: 0,//国内机票，火车票，
    /**
     * 国内机票
     */
    fromCity: {
      'CityCode': "SHA",
      'CityName': "上海",
      'Continent': "亚洲",
      'Country': "中国",
      'CountryCode': "CN",
      'EnglishName': "shanghai",
      'FlightCityID': 491,
      'IsAbroadHot': false,
      'IsInternal': true,
      'IsInternalHot': true,
      'PinYin': "shanghaihongqiao",
      'ShortChar': "sh",
      'Tag': null,
      'TopSort': 0
    },
    toCity: {
      'CityCode': "BJS",
      'CityName': "北京",
      'Continent': "亚洲",
      'Country': "中国",
      'CountryCode': "CN",
      'EnglishName': "beijing",
      'FlightCityID': 130,
      'IsAbroadHot': false,
      'IsInternal': true,
      'IsInternalHot': true,
      'PinYin': "beijing",
      'ShortChar': "bj",
      'Tag': null,
      'TopSort': 0
    },
    goDate: '',
    backDate: '',
    goWeek: '',
    backWeek: '',


    /**
     * 火车票
     */
    T_goData: '',
    T_goWeek: '',
    T_fromCity: {
      'Abbreviation': "sh",
      'HotSort': 0,
      'Id': 9,
      'IsHot': false,
      'Name': "上海",
      'PinYin': "shanghai",
      'StationCode': "SHH",
      'Tag': null
    },
    T_toCity: {
      'Abbreviation': "bj",
      'HotSort': 0,
      'Id': 3,
      'IsHot': false,
      'Name': "北京",
      'PinYin': "beijing",
      'StationCode': "BJP",
      'Tag': null
    },
    T_isSelectHighSpeedTrain: false,//是否选择只查高铁动车
  },
  onLoad: function () {
    var that = this;
    var dateStr = util.getNowFormatDate();
    console.log('now:' + dateStr);
    var dateBefore = util.getTheDayBeforeDate(dateStr, true);
    console.log('before:' + dateBefore);
    var week = util.getTheDateWeek(dateStr);
    console.log('week：' + week);


    that.setData({
      goDate: dateStr,
      goWeek: week,
      T_goDate: dateStr,
      T_goWeek: week,
    })

    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()

  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  //搜索---国内机票
  searchList: function () {
    var that = this;
    if (app.globalData.TUserInfo == null) {
      wx.navigateTo({
        url: '../bindphone/bindphone',
      })
      return;
    }
    if (that.data.fromCity == null) {
      wx.showToast({
        title: '请选择出发城市',
      })
      return;
    }
    if (that.data.toCity == null) {
      wx.showToast({
        title: '请选择到达城市',
      })
      return;
    }
    wx.navigateTo({
      url: '../flight/searchList/searchList?goDate=' + that.data.goDate + '&goWeek=' + that.data.goWeek + '&fromCity=' + JSON.stringify(that.data.fromCity) + '&toCity=' + JSON.stringify(that.data.toCity)
    })

  },
  //选择出发，到达城市
  city: function (event) {
    var that = this;
    var ticketType = event.currentTarget.dataset.type;//0：火车票，1：国内机票
    var isfromcity = event.currentTarget.dataset.isfrom;//1：出发，0：到达
    console.log(ticketType,isfromcity);
    wx.navigateTo({
      url: '../city/city?type=' + ticketType + '&isfromcity=' + isfromcity
    })
  },
  //选择出发，返程日期
  date: function (event) {
    var that = this;
    var ticketType = event.currentTarget.dataset.type;//0：火车票，1：国内机票
    wx.navigateTo({
      url: '../dateSelect/dateSelect?startDate=' + that.data.goDate + '&isStartDate=' + 1 + '&type=' + ticketType
    })
  },
  //切换购票类型，国内机票-火车票
  changedTicketType: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    console.log(index);
    that.setData({
      selectIndex: index
    })
  },
  //交换出发到达城市—国内
  changeTheCity: function () {
    var that = this;
    var mid = that.data.fromCity;
    that.setData({
      fromCity: that.data.toCity,
      toCity: mid
    })
  },
  //交换出发到达城市-火车票
  t_changeTheCity: function () {
    var that = this;
    var mid = that.data.T_fromCity;
    that.setData({
      T_fromCity: that.data.T_toCity,
      T_toCity: mid
    })
  },
  //只查看高铁按钮,默认为不选中.
  t_changeTheSelectOfHighSpeedTrain: function () {
    var that = this;
    that.data.T_isSelectHighSpeedTrain = !that.data.T_isSelectHighSpeedTrain;
    console.log(that.data.T_isSelectHighSpeedTrain);
  },
  bindphone: function () {

    wx.navigateTo({
      url: '../bindphone/bindphone',
    })


  },
  /**
   * 日期回调函数_国内机票
   * dateStr:日期
   * weekStr:星期几
   */
  updateTicketStartDate: function (dateStr, weekStr) {
    var that = this;
    that.setData({
      goDate: dateStr,
      goWeek: weekStr,
    })
  },
  /**
   * 日期回调函数_火车票
   * dateStr:日期
   * weekStr:星期几
   */
  updateTrainStartDate: function (dateStr, weekStr) {
    var that = this;
    that.setData({
      T_goDate: dateStr,
      T_goWeek: weekStr,
    })
  },
  /**
   * 选择城市回调_国内机票
   * selectData:返回已选择城市
   * typeStr:'0',国内出发;'1'，国内到达;'2',火车票出发；'3',火车票到达.
   */
  updateSelectdCity: function (selectData, typeStr) {
    var that = this;
    console.log('选择城市回调');
    
    if (typeStr == '0') {
      console.log('国内出发');
      that.setData({
        fromCity: selectData
      })
    }
    else if (typeStr == '1') {
      console.log('国内到达');
      that.setData({
        toCity: selectData
      })
    }
    else if (typeStr == '2') {
      console.log('火车票出发');
      that.setData({
        T_fromCity: selectData
      })
    }
    else if (typeStr == '3') {
      console.log('火车票到达');
      that.setData({
        T_toCity: selectData
      })
    }
  },
  hotelTap:function() {
    console.log('11111');
    this.wetoast.toast({
      title: '该功能尚未开通,敬请期待!',
      duration: 1000
    })
  },
  taxTap:function() {
    this.wetoast.toast({
      title: '该功能尚未开通,敬请期待!',
      duration: 1000
    })
  },
  insuranceTap(){
    this.wetoast.toast({
      title: '该功能尚未开通,敬请期待!',
      duration: 1000
    })
  },
  //搜索---火车票
  trainsearchList: function () {
    var that = this;
    if (app.globalData.TUserInfo == null) {
      wx.navigateTo({
        url: '../bindphone/bindphone',
      })
      return;
    }
    wx.navigateTo({
      url: '../train/searchList/searchList?T_fromCity=' + JSON.stringify(that.data.T_fromCity) + '&T_toCity=' + JSON.stringify(that.data.T_toCity) + '&T_goDate=' + that.data.T_goDate + '&T_goWeek=' + that.data.T_goWeek + '&T_isSelectHighSpeedTrain=' + that.data.T_isSelectHighSpeedTrain
    })
  }
})
